﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Xml;
using System.Reflection;

namespace WebMacros
{
    public partial class Form1 : Form
    {
        private Thread m_scriptThread;

        public Form1()
        {
            InitializeComponent();
            m_scriptThread = new Thread( ThreadMain );
        }

        private void ThreadMain()
        {
            XmlDocument xml = new XmlDocument();
            xml.Load( Directory.GetCurrentDirectory() + "\\XMLFile1.xml" );

            Assembly asm = Assembly.GetEntryAssembly();

            foreach (XmlNode node in xml["root"].ChildNodes)
            {
                if (node.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                string id = node.Attributes["id"].Value;
                string pass = node.Attributes["pass"].Value;

                string reflectionName = asm.GetName().Name + "." + node.Name;

                textBox2.Invoke(new MethodInvoker(delegate()
                {
                    textBox2.AppendText(string.Format("class : {0} \r\n", reflectionName));
                    textBox2.AppendText(string.Format("id : {0}, pass : {1} \r\n", id,pass));
                }));

                Type type = Type.GetType(reflectionName);
                Object obj = Activator.CreateInstance(type, new object[] { textBox2, id, pass });
            }
        }

        private void Form1_Load( object sender, EventArgs e )
        {
            m_scriptThread.Start();

            Scripter.StartButton();
        }
        
        private void Exit()
        {
            m_scriptThread.Abort();
            m_scriptThread.Join();
            Scripter.StopButton();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Exit();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Exit();
        }
    }
}

